<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
	//1.采用直接量的方式创建对象
	function f1() {
		var student = {"name":"zhangsan","age":25,
			"job":function(){alert("我学Java")}};
		alert(student.name);
		alert(student.age);
		student.job();
	}
	//2.1采用JS内置构造器创建对象
	//在JS里用来new的函数就是构造器
	function f2() {
		var teacher = new Object();
		teacher.name = "cang";
		teacher.age = 18;
		teacher.job = function(){
			alert("我教Java");
		};
		
		alert(teacher.name);
		alert(teacher.age);
		teacher.job();
	}
	//2.2采用自定义构造器创建对象
	//1)定义构造器
	//2)声明参数
	function Coder(name,age,job) {
		//this代表当前对象
		//this.name是给此对象加1个属性叫name
		//=name是将参数name赋值给这个属性
		this.name = name;
		this.age = age;
		this.work = job;
	}
	//3)new这个构造器获得一个对象
	function f3() {
		var coder = new Coder("lisi",26,
			function(){alert("我写Java");});
		alert(coder.name);
		alert(coder.age);
		coder.work();
	}
</script>
</head>
<body>
	<input type="button" value="按钮1"
		onclick="f1();"/>
	<input type="button" value="按钮2"
		onclick="f2();"/>
	<input type="button" value="按钮3"
		onclick="f3();"/>
</body>
</html>





